﻿using BlogApi.Data;
using BlogApi.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogApi.Services
{
    public class ArticleToArticleTagRepository:IArticleToArticleTagRepository
    {
        private readonly BlogDbContext _context;

        public ArticleToArticleTagRepository(BlogDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }
        public async Task<IEnumerable<ArticleToArticleTag>> GetArticleToArticleTagsAsync()
        {
            return await _context.ArticleToArticleTags_table.ToListAsync();
        }
        public async Task<ArticleToArticleTag> GetArticleToArticleTagAsync(Guid articleToArticleTagId)
        {
            if (articleToArticleTagId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(articleToArticleTagId));
            }
            return await _context.ArticleToArticleTags_table.FirstOrDefaultAsync(m => m.Id == articleToArticleTagId);
        }
        public async Task<IEnumerable<ArticleToArticleTag>> GetArticleToArticleTagsByArticleIdAsync(Guid articleId)
        {
            if (articleId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(articleId));
            }
            return await _context.ArticleToArticleTags_table.Where(m => m.ArticleId == articleId).ToListAsync();
        }
        public async Task<IEnumerable<ArticleToArticleTag>> GetArticleToArticleTagsByTagIdAsync(Guid tagId)
        {
            if (tagId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(tagId));
            }
            return await _context.ArticleToArticleTags_table.Where(m => m.ArticleTagId == tagId).ToListAsync();
        }
        public async Task<IEnumerable<ArticleToArticleTag>> GetArticleToArticleTagsByTagIdAsync(Guid tagId, int pageSize, int pageIndex)
        {
            if (tagId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(tagId));
            }
            return await _context.ArticleToArticleTags_table.Where(m => m.ArticleTagId == tagId).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public void AddArticleToArticleTag(ArticleToArticleTag articleToArticleTag)
        {
            if (articleToArticleTag == null)
            {
                throw new ArgumentNullException(nameof(articleToArticleTag));
            }
            _context.ArticleToArticleTags_table.Add(articleToArticleTag);
        }
        public void UpdateArticleToArticleTag(ArticleToArticleTag articleToArticleTag)
        {
            //由于数据库上下文会对实体进行跟踪 所以这句话可以不写
            //_context.Entry(articleToArticleTag).State = EntityState.Modified;
        }
        public void DeleteArticleToArticleTag(ArticleToArticleTag articleToArticleTag)
        {
            if (articleToArticleTag == null)
            {
                throw new ArgumentNullException(nameof(articleToArticleTag));
            }
            _context.ArticleToArticleTags_table.Remove(articleToArticleTag);
        }
        public async Task<bool> ArticleToArticleTagExistsAsync(Guid articleToArticleTagId)
        {
            if (articleToArticleTagId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(articleToArticleTagId));
            }
            return await _context.ArticleToArticleTags_table.AnyAsync(m => m.Id == articleToArticleTagId);
        }
        public async Task<bool> SaveAsync()
        {
            return await _context.SaveChangesAsync() >= 0;
        }
    }
}
